package com.zwk.sta.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zwk.base.entity.CommonResult;
import com.zwk.sta.client.MemberClient;
import com.zwk.sta.entity.Daily;
import com.zwk.sta.mapper.DailyMapper;
import com.zwk.sta.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author zwk
 * @since 2022-09-02
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private DailyMapper dailyMapper;

    @Autowired
    private MemberClient memberClient;

    // 查询某天的注册人数, 加进数据库
    @Override
    public void registerCount(String day) {
        CommonResult commonResult = memberClient.countRegister(day);
        Integer countRegister = (Integer) commonResult.getData().get("countRegister");

        Daily daily = new Daily();
        daily.setRegisterNum(countRegister);
        daily.setDateCalculated(day);

        daily.setCourseNum(0);
        daily.setLoginNum(0);
        daily.setVideoViewNum(0);

        // 存入数据库, 先查询数据库中是否存在当天数据, 存在就修改
        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", day);
        Integer count = dailyMapper.selectCount(wrapper);
        if (count > 0) {
            dailyMapper.update(daily, wrapper);
            return;
        }
        dailyMapper.insert(daily);
    }

    // 图表显示, 返回需要查询的数据
    @Override
    public Map<String, Object> showData(String type, String begin, String end) {
        // 判断begin和end大小
        if (begin.compareTo(end) > 0){
            String temp = begin;
            begin = end;
            end = temp;
        }

        QueryWrapper<Daily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.select("date_calculated", type);
        List<Daily> dailyList = dailyMapper.selectList(wrapper);

        // 封装数据
        List<String> dataList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();
        for (Daily daily: dailyList
             ) {
            dataList.add(daily.getDateCalculated());
            switch (type) {
                case "login_num":
                    countList.add(daily.getLoginNum());
                    break;
                case "register_num":
                    countList.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    countList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    countList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("date_calculatedList", dataList);
        map.put("numDataList", countList);

        return map;
    }
}
